package com;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class GameMain extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	Font ft = new Font("华文彩云", Font.LAYOUT_NO_LIMIT_CONTEXT, 16);
	private JLabel jlone;
	private JLabel jltwo;
	private JLabel jlthree;
	private JLabel jlfour;
	private JLabel jlfive;
	private JLabel jlsix;
	private SnakePn sp;
	private Snake snake;
	private boolean isStart = false;
	private boolean space = false;

	public GameMain() {
		snake = new Snake();
		initcomponents();
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_UP) {
					snake.changeDirection(Snake.UP);
				} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
					snake.changeDirection(Snake.DOWN);

				} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
					snake.changeDirection(Snake.LEFT);
				} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
					snake.changeDirection(Snake.RIGHT);
				} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
					if (space == false) {
						snake.setState(Snake.PAUSE);
						space = true;
					} else if (space == true) {
						snake.setState(Snake.RUNNING);
						space = false;
					}
				}
				return;
			}
		});
	}

	public void setLabelSpeed(int speed) {
		jltwo.setText(String.valueOf(speed));
	}

	private void initcomponents() {
		JMenuBar mb = new JMenuBar();
		setJMenuBar(mb);
		setSize(800, 555);
		setResizable(false);
		Dimension di = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((di.width - 800) / 2, (di.height - 570) / 2);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("贪吃蛇");
		setLayout(null);
		jlone = new JLabel("速度:");
		jlone.setBounds(600, 100, 50, 25);
		jlone.setFont(ft);
		add(jlone);
		jltwo = new JLabel("");
		jltwo.setBounds(650, 100, 50, 25);
		jltwo.setFont(ft);
		add(jltwo);
		jlthree = new JLabel("状态:");
		jlthree.setBounds(600, 180, 50, 25);
		jlthree.setFont(ft);
		add(jlthree);
		jlfour = new JLabel("");
		jlfour.setBounds(650, 180, 100, 25);
		jlfour.setFont(ft);
		add(jlfour);
		jlfive = new JLabel("分数:");
		jlfive.setBounds(600, 260, 50, 25);
		jlfive.setFont(ft);
		add(jlfive);
		jlsix = new JLabel("");
		jlsix.setBounds(650, 260, 50, 25);
		jlsix.setFont(ft);
		add(jlsix);
		String[] menu = { "游戏", "难度", "关于" };
		String[][] submenu = { { "新游戏", "暂停", "继续", "结束" }, { "高级", "中级", "低级" }, { "帮助" } };
		for (int i = 0; i < menu.length; i++) {
			JMenu m = new JMenu(menu[i]);
			for (int j = 0; j < submenu[i].length; j++) {
				JMenuItem mi = new JMenuItem(submenu[i][j]);
				mi.setFont(ft);
				mi.addActionListener(this);
				m.add(mi);
			}
			m.setFont(ft);
			mb.add(m);
		}
		sp = new SnakePn(snake);
		sp.setBounds(0, 0, 500, 500);
		sp.setBorder(BorderFactory.createBevelBorder(1));
		sp.setBackground(new Color(0xFF00FF));
		add(sp);

	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				new GameMain();

			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String com = e.getActionCommand();
		if (com.equals("新游戏")) {
			if (isStart == false || snake.getState() == Snake.OVER) {
				remove(sp);
				snake = new Snake();
				sp = new SnakePn(snake);
				sp.setBounds(0, 0, 500, 500);
				sp.setBorder(BorderFactory.createBevelBorder(1));
				sp.setBackground(new Color(0xFF00FF));
				add(sp);
				this.jlfour.setText("运行");
				this.setLabelSpeed(500);
				snake.setState(Snake.RUNNING);
				snake.setSpeed(500);
				Th t1 = new Th(snake, sp, jlsix, jlfour);
				t1.start();
				isStart = true;

			}
		} else if (com.equals("暂停")) {
			snake.setState(Snake.PAUSE);
			this.jlfour.setText("暂停");
		} else if (com.equals("继续")) {
			this.jlfour.setText("运行");
			snake.setState(Snake.RUNNING);
		} else if (com.equals("结束")) {
			snake.setState(Snake.OVER);

		} else if (com.equals("高级")) {
			snake.setSpeed(200);
			this.setLabelSpeed(700);
		} else if (com.equals("中级")) {
			snake.setSpeed(500);
			this.setLabelSpeed(500);
		} else if (com.equals("低级")) {
			snake.setSpeed(700);
			this.setLabelSpeed(200);
		}
	}
}
